package md.frolov.bible.toolbars;


import md.frolov.bible.commands.GoToURLCommand;
import md.frolov.bible.commands.OpenEditorCommand;
import md.frolov.bible.editors.BibleEditorInput;
import md.frolov.bible.editors.MainEditor;

import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;

public class NavigationTextContribution extends	WorkbenchWindowControlContribution {
	public final static String ID = "md.frolov.bible.toolbars.NavigationTextContribution";
	private Text text;
	private boolean justFocused = false;
	
	public NavigationTextContribution() {
		// TODO Auto-generated constructor stub
	}

	public NavigationTextContribution(String id) {
		super(id);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Control createControl(Composite parent) {
		text = new Text(parent,SWT.BORDER);
		
		text.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode!=13) return;
				if((e.stateMask & SWT.SHIFT) > 0){
					OpenEditorCommand.executeStatic(null);
					return;
				}
				GoToURLCommand.executeStatic(null);
			}

			@Override
			public void keyReleased(KeyEvent e) {
				//do nothing
			}
			
		});
		
		text.addMouseListener(new MouseListener(){

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseDown(MouseEvent e) {
				if(justFocused){
					text.setSelection(0,text.getText().length());
					justFocused = false;
					return;
				}
			}

			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		text.addFocusListener(new FocusListener(){

			@Override
			public void focusGained(FocusEvent e) {
				justFocused = true;
			}

			@Override
			public void focusLost(FocusEvent e) {
				text.setSelection(0);
			}
			
		});
		
		return text;
	}

	@Override
	protected int computeWidth(Control control) {
		return 300;
	}

	public String getText(){
		return text.getText();
	}
	
	public void setText(String str){
		text.setText(str);
	}
	
	public static NavigationTextContribution getTextContribution(){
		WorkbenchWindow wb = (WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		ICoolBarManager cb = wb.getCoolBarManager();
		ToolBarContributionItem tbci = (ToolBarContributionItem) cb.find("md.frolov.bible.toolbars.navigaion");
		NavigationTextContribution ntc = (NavigationTextContribution) tbci.getToolBarManager().find(ID);
		return ntc;
	}
	
	public static void refresh(){
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(editor==null) return;
		if(!(editor instanceof MainEditor)) return;
		
		MainEditor mEditor = (MainEditor) editor;
		getTextContribution().setText(((BibleEditorInput)mEditor.getEditorInput()).getYok());
	}
	
	public static void focus(){
		NavigationTextContribution.getTextContribution().setFocus();
	}
	
	private void setFocus(){
		text.setSelection(0,text.getText().length());
		text.setFocus();
	}
}
